package com.symone.webcms.model.vo;

import com.symone.webcms.model.DataContent;
import com.symone.webcms.util.CommonUtil;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.Data;

import java.util.*;

@Data
public class DataContentVO {
    private Long id;
    private String lang;
    private String module;
    private Long groupId;
    private String groupName;
    private Map<String,Object> vdata;
    private String views;
    private Integer state;
    private Boolean deleted;
    private String createUser;
    private Date createTime;
    private String updateUser;
    private Date updateTime;

    public DataContentVO() {
    }

    public DataContentVO(DataContent content){
        this.id = content.getId();
        this.lang = content.getLang();
        this.module = content.getModule();
        this.groupId = content.getGroupId();
        this.vdata = new HashMap<>();
        this.views = content.getViews();
        this.state = content.getState();
        this.deleted = content.getDeleted();
        this.createUser = content.getCreateUser();
        this.createTime = content.getCreateTime();
        this.updateUser = content.getUpdateUser();
        this.updateTime = content.getUpdateTime();

        this.vdata.put("id",this.id);
        this.vdata.put("lang",this.lang);
        this.vdata.put("module",this.module);
        this.vdata.put("group_id",this.groupId);
        this.vdata.put("group_name",this.groupName);
        this.vdata.put("state",this.state);
        if (StrUtil.isNotEmpty(this.views)){
            JSONObject json = JSONUtil.parseObj(this.views);
            for (Object key : json.keySet()) {
                Object val = json.get(key);
                this.vdata.put((String)key, val);
            }
        }
    }

    public Map<String,Object> getData(){
        if( vdata == null || vdata.size() <= 0 ) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", id);
            data.put("lang", lang);
            data.put("module", module);
            data.put("group_id", groupId);
            data.put("group_name", groupName);
            data.put("state", state);
            if (StrUtil.isNotEmpty(views)) {
                JSONObject json = JSONUtil.parseObj(views);
                for (Object key : json.keySet()) {
                    Object val = json.get(key);
                    data.put((String) key, val);
                }
            }
            return data;
        }else {
            return vdata;
        }
    }
}
